# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/9/10 16:00
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : nibabel_io.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
import nibabel as nib
from matplotlib import pyplot as plt, font_manager
import matplotlib.image as mpimg
import numpy as np
my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc")
# 读取.nii图像
nii_fig = nib.load("../experiment_fig/KKI2009-01-MPRAGE.nii")
# 打印图像的大小,(170, 256, 256)
print("nii图像大小:{}".format(nii_fig.shape))
# 由于get_data()函数即将被淘汰，运行显示警告，故采用get_fdata()
ndarray_obj = np.array(nii_fig.get_fdata())
# 获取横断面、冠状面、矢状面的图像
transverse = ndarray_obj[:, :, 120]
coronal = ndarray_obj[:, 120, :]
sagittal_plane = ndarray_obj[120, :, :]
# 显示三个方向的图像
plt.figure('nii', figsize=(10, 10))  # 窗口名称和大小
plt.subplot(1, 3, 1), plt.title('横断面', fontproperties=my_font), plt.imshow(transverse,cmap="gray"), plt.axis('off')
plt.subplot(1, 3, 2), plt.title('冠状面', fontproperties=my_font), plt.imshow(coronal,cmap="gray"), plt.axis('off')
plt.subplot(1, 3, 3), plt.title('矢状面', fontproperties=my_font), plt.imshow(sagittal_plane,cmap="gray"), plt.axis('off')
plt.show()
# 将显示的三个方向的图像保存
mpimg.imsave("./../experiment_fig/hdm.png", transverse)
mpimg.imsave("./../experiment_fig/gzm.png", coronal)
mpimg.imsave("./../experiment_fig/szm.png", sagittal_plane)
